package orm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.u2b.db.datasource.DbcpHandler;
import com.u2b.global.utils.CheckParameter;
import com.u2b.servlet.utils.ParameterParser;

public class QueryProxy
{
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs = null;
    private final ParameterParser parameterParser;
    public QueryProxy()
    {
        try
        {
            conn = DbcpHandler.getInstance().getReadConnection();
        }
        catch (Exception e)
        {
            throw new RuntimeException("连接池获得异常", e);
        }
        parameterParser = new ParameterParser();
    }

    public List<HashMap<String, Object>> query(String sql, String[] sqlKeys, HashMap<String, Object> value)
    {

        Object[] SQLvalues = parameterParser.getSQLValuesFromMap(sqlKeys, value);
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

        try
        {
            ps = conn.prepareStatement(sql);
            CheckParameter.checkParameterNumber(SQLvalues.length, ps);
            for (int i = 0; i < SQLvalues.length; i++)
            {
                ps.setObject(i + 1, SQLvalues[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rmd = rs.getMetaData();
            int count = rmd.getColumnCount();
            String[] columns = new String[count];
            for (int i = 0; i < count; i++)
            {
                columns[i] = rmd.getColumnName(i + 1);
            }
            while (rs.next())
            {
                HashMap<String, Object> singleValue = new HashMap<String, Object>();
                for (int i = 0; i < count; i++)
                {
                    singleValue.put(columns[i], rs.getObject(i + 1));
                   
                }
                data.add(singleValue);
                singleValue = null;
            }
        }
        catch (SQLException e)
        {
            new RuntimeException("查询数据时出现异常", e);
        }
        finally
        {
            DbcpHandler.free(conn, null, null);
        }
        return data;
    }

    public List<HashMap<String, Object>> query(String sql, int someId)
    {
        ResultSet rs = null;
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

        try
        {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, someId);
            rs = ps.executeQuery();
            ResultSetMetaData rmd = rs.getMetaData();
            int count = rmd.getColumnCount();//
            String[] columns = new String[count];
            for (int i = 0; i < count; i++)
            {
                columns[i] = rmd.getColumnName(i + 1);
            }
            while (rs.next())
            {
                HashMap<String, Object> e = new HashMap<String, Object>();
                for (int i = 0; i < count; i++)
                {
                    e.put(columns[i], rs.getObject(i + 1));
                }
                data.add(e);
                e = null;
            }
        }
        catch (Exception se)
        {
            new RuntimeException("查询数据时出现异常", se);
        }
        finally
        {
           DbcpHandler.free(conn, ps, rs);
        }
        return data;

    }
    
}
